from typing import Optional

from dashscope import Generation
from langchain_core.messages import HumanMessage
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.utils.function_calling import convert_to_openai_tool


class Person(BaseModel):
    """Information about a person."""
    name: Optional[str] = Field(default=None, description="The name of the person")
    hair_color: Optional[str] = Field(default=None, description="The color of the peron's hair if known")
    height_in_meters: Optional[str] = Field(default=None, description="Height measured in meters")

print("-----------------convert_to_openai_tool--------------------------------")
print(convert_to_openai_tool(Person))

prompt = ChatPromptTemplate.from_messages(
    [
        (
            "system",
            "You are an expert extraction algorithm. "
            "Only extract relevant information from the text. "
            "If you do not know the value of an attribute asked to extract, "
            "return null for the attribute's value.",
        ),
        ("human", "{text}"),
    ]
)

def get_response_1t(mess):
    response = Generation.call(
        model='qwen-plus',
        messages=mess,
        api_key="sk-c78e46dbb3824fd2bb26c3bcd4bbc2a6",
        tools=[convert_to_openai_tool(Person)],
        result_format='message', # 将输出设置为message形式
    )
    return response

def prompt_ty(input):
    pro_sys="You are an expert extraction algorithm.Only extract relevant information from the text.If you do not know the value of an attribute asked to extract,return null for the attribute's value."
    return [{"role":"system","content":pro_sys},{"role":"user","content":input}]


print("-----------------get_response_1t--------------------------------")
p_y=prompt_ty("Alan Smith is 6 feet tall and has blond hair.")
res=get_response_1t(p_y)
print(res.output.choices[0].message["tool_calls"][0])
# res=get_response_1t(p_y)
# print(prompt.invoke(
#     {"text": "this is some text", "examples": [HumanMessage(content="testing 1 2 3")]}
# ))
